import { getItem, setItem } from '@/utils/auth.js'
import { userProfile } from '@/api/user.js'

const state = {
  token: getItem() || '',
  userInfo: '',
  refresh_token: localStorage.getItem('refresh_token') || ''
}
const actions = {
  async getUserInfo (store) {
    const res = await userProfile()
    // console.log(res)
    store.commit('setUserInfo', res.data)
  }
}
const getters = {
  getToken (state) {
    return state.token
  }
}
const mutations = {
  setToken (state, value) {
    state.token = value
    setItem(value)
  },
  setUserInfo (state, value) {
    state.userInfo = value
  },
  setUserOther (state, value) {
    state.userInfo[value.key] = value.value
  },
  setRefreshToken (state, value) {
    state.refresh_token = value
    localStorage.setItem('refresh_token', value)
  }
  // setP (state, value) {
  //   state.userInfo.photo = value
  // },
  // setB (state, value) {
  //   state.userInfo.birthday = value
  // }
}
export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
